var blogCategory = function(){
    var blogCategrory = this;
    this.URL = {
        list:"/blogCategory/list",
        addCategory: "/blogCategory/add",
        updateCategory: "/blogCategory/update",
        deleteCategory: function (id){
            return "/blogCategory/delete/"+id
        },
        listCategoryTree: "/blogCategory/listCategoryTree"
    }
    this.isUpdateOrAdd = 0; //默认新增
    this.dataGridInit = function (){
        $("#dataCategoryList").treegrid({
            url:blogCategrory.URL.list,
            method:"GET",
            singleSelect:true,
            checkOnSelect:true,
            fitColumns:true,
            fit:true,
            border:false,
            toolbar:'#tt',
            idField:'id',
            treeField:'name',
            lines:true,
            columns:[[
                {field:'id', hidden:true },
                {field:'_parentId',hidden:true },
                {field:'name',title:'分类名称',width:200 },
                {field:'createTime',title:'创建时间',width:200 }
            ]]
        })
    }
    this.buttonInit = function(){
        //新建按钮
        $("#newCategory").click(function (){
            var row = $("#dataCategoryList").datagrid("getSelected");
            if(row){
                $("#nameTop").textbox("setValue",row.id);
                $("#nameTop").textbox("setText",row.name);
            }
                blogCategrory.isUpdateOrAdd = 0;
                $("#addCategoryDialog").dialog("setTitle","添加分类");
                $("#addCategoryDialog").dialog("open");
        });
        //修改按钮
        $("#updateCategory").click(function (){
            var row = $("#dataCategoryList").datagrid("getSelected");
            if(row){
                $('#nameTop').combotree('setValue',row._parentId);
                $("#name").textbox('setText',row.name);
                blogCategrory.isUpdateOrAdd = 1;
                $("#addCategoryDialog").dialog("setTitle","修改分类");
                $("#addCategoryDialog").dialog("open");
            }else {
                $.messager.alert("提示","请先选中一行！")
            }

        });
        //删除按钮
        $("#deleteCategory").click(function (){
            var row = $("#dataCategoryList").datagrid("getSelected");
            if(row){
                $.messager.confirm("提示","是否删除？",function(e){
                    if(e){
                        $.post(blogCategrory.URL.deleteCategory(row.id),function(data){
                            var dataobj = eval('(' + JSON.stringify(data) + ')'); // change the JSON string to javascript object
                            if(dataobj.type == 'success'){
                                $("#dataCategoryList").treegrid("reload")
                            }else if(dataobj.type == 'error'){
                                $.messager.alert("提示",dataobj.text);
                            }else {
                                $.messager.alert("提示","未知错误");
                            }
                        })
                    }
                });
            }else {
                $.messager.alert("提示","请先选中一行！")
            }

        });
        ///保存按钮
        $("#saveCategoryBtn").click(function () {
            var row = $("#dataCategoryList").datagrid("getSelected");
            var url = blogCategrory.URL.addCategory;
            if(blogCategrory.isUpdateOrAdd == 1){
                url = blogCategrory.URL.updateCategory;
            }
            $("#addCategoryForm").form("submit",{
                url:url,
                onSubmit:function(param){
                    if(blogCategrory.isUpdateOrAdd == 1){
                        param.id  = row.id
                    }
                    return $(this).form('enableValidation').form('validate');
                },
                success:function (data){
                    var dataobj = eval('(' + data + ')'); // change the JSON string to javascript object
                    if(dataobj.type == 'success'){
                        $('#addCategoryDialog').dialog('close');
                        $("#dataCategoryList").treegrid("reload")
                    }else if(dataobj.type == 'error'){
                        $.messager.alert("提示",dataobj.text);
                    }else {
                        $.messager.alert("提示","未知错误");
                        $('#addCategoryDialog').dialog('close');
                    }
                }
            })
        })

        //=============================弹出框的树结构============================================//
        $("#nameTop").combotree({
            method:'post',
            url: blogCategrory.URL.listCategoryTree,
            parentField:"pid",
            textFiled:"name",
            editable:true,
            lines:true
        });
    }
    this.init = function(){
        blogCategrory.dataGridInit();
        blogCategrory.buttonInit();
    }
}

$(function(){
    new blogCategory().init();
})